﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Threading;

namespace QLTV.Forms.KhaiBao
{

    public partial class frmCrewList : BaseForm
    {
        public frmCrewList()
        {
            InitializeComponent();
        }
        Forms.frmPleaseWait _wait;
        private void frmCrewList_Load(object sender, EventArgs e)
        {
           
          
            Thread lengthyProcessThread = new Thread(loadData) { IsBackground = true };
            lengthyProcessThread.Start();
            _wait = new Forms.frmPleaseWait();
            _wait.ShowDialog();

        }
        void loadData()
        {
          


            // TODO: This line of code loads data into the 'dsCrew.tblCertification' table. You can move, or remove it, as needed.
            this.tblCertificationTableAdapter.Fill(this.dsCrew.tblCertification);
            // TODO: This line of code loads data into the 'dsCrew.tblCertificateCrew' table. You can move, or remove it, as needed.
            this.tblCertificateCrewTableAdapter.Fill(this.dsCrew.tblCertificateCrew);
            // Start my lengthy Process
            // TODO: This line of code loads data into the 'dsVessel.tblEngineType' table. You can move, or remove it, as needed.
            this.tblEngineTypeTableAdapter.Fill(this.dsVessel.tblEngineType);
            // TODO: This line of code loads data into the 'dsVessel.tblEngineMaker' table. You can move, or remove it, as needed.
            this.tblEngineMakerTableAdapter.Fill(this.dsVessel.tblEngineMaker);
            // TODO: This line of code loads data into the 'dsVessel.tblVesselType' table. You can move, or remove it, as needed.
            this.tblVesselTypeTableAdapter.Fill(this.dsVessel.tblVesselType);
            // TODO: This line of code loads data into the 'dsVessel.tblFlag' table. You can move, or remove it, as needed.
            this.tblFlagTableAdapter.Fill(this.dsVessel.tblFlag);
            // TODO: This line of code loads data into the 'dsVessel.tblVessel' table. You can move, or remove it, as needed.
            this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            // TODO: This line of code loads data into the 'dsCrew.tblCourse' table. You can move, or remove it, as needed.
            this.tblCourseTableAdapter.Fill(this.dsCrew.tblCourse);
            // TODO: This line of code loads data into the 'dsCrew.tblEducationType' table. You can move, or remove it, as needed.
            this.tblEducationTypeTableAdapter.Fill(this.dsCrew.tblEducationType);
            // TODO: This line of code loads data into the 'dsCrew.tblImmigrationDoc' table. You can move, or remove it, as needed.
            this.tblImmigrationDocTableAdapter.Fill(this.dsCrew.tblImmigrationDoc);
            // TODO: This line of code loads data into the 'dsRanking.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsRanking.tblRank);
            // TODO: This line of code loads data into the 'dsRanking.tblRanking' table. You can move, or remove it, as needed.
            // TODO: This line of code loads data into the 'dsCrew.tblCrew' table. You can move, or remove it, as needed.
            this.tblCrewTableAdapter.Fill(dsCrew.tblCrew);
             tblCrewBindingSource.Filter = "bDeleted = 0";
            //tblCrewBindingSource.ResetCurrentItem();

             // TODO: This line of code loads data into the 'dsAssignment.tblAssignment' table. You can move, or remove it, as needed.


            _wait.ShouldCloseNow = true;




        }
        public string getCrewID()
        {
            if (dsCrew.tblCrew.Rows.Count > 0)
            {
                var data = from crew in dsCrew.tblCrew.AsEnumerable()
                           orderby crew.Field<string>("nCrewId").Substring(1) descending
                           select crew.Field<string>("nCrewId").Substring(1);

                int maxID = Convert.ToInt32(data.First<string>());

                if (maxID > 0 && maxID < 10)
                {
                    return "000" + (maxID + 1).ToString();

                }
                if (maxID >= 10 && maxID < 100)
                {
                    return "00" + (maxID + 1).ToString();

                }
                if (maxID >= 100 && maxID < 1000)
                {
                    return "0" + (maxID + 1).ToString();

                }
                if (maxID >= 1000)
                {
                    return maxID.ToString();

                }
            }
            else
                return "0001";
            return "";
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmCrew f = new frmCrew();
            f.StartPosition = FormStartPosition.CenterParent;
            f.isNew = true;
            f.newCrewid = getCrewID();
            f.ShowDialog();
            tblCrewBindingSource.DataSource = tblCrewTableAdapter.GetData();
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle >= 0)
            {
                frmCrew f = new frmCrew();
                f.StartPosition = FormStartPosition.CenterParent;
                f.isNew = false;
                f.crewid = viewCrew.GetFocusedRowCellValue("nCrewId").ToString();
                f.ShowDialog();
                tblCrewBindingSource.DataSource = tblCrewTableAdapter.GetData();
            }
        }

        private void viewRank_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

        private void splitterItem1_DoubleClick(object sender, EventArgs e)
        {
            //DevExpress.XtraLayout.Customization.LayoutItemDragController controler= new DevExpress.XtraLayout.Customization.LayoutItemDragController()

            //splitterItem1.Move()
        }

        private void splitContainerControl1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            splitContainerControl1.SplitterPosition = splitContainerControl1.Height - 30;
        }

        private void viewRank_FocusedRowChanged_1(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (viewCrew.GetFocusedRowCellValue("nCrewId") != null)
            {
                tblImmigrationCrewBindingSource.DataSource = tblImmigrationCrewTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblEducationBindingSource.DataSource = tblEducationTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                // TODO: This line of code loads data into the 'dsCrew.tblExprience' table. You can move, or remove it, as needed.
                tblExprienceBindingSource.DataSource = this.tblExprienceTableAdapter.GetDataBy(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblNhanXetCuaChuTauBindingSource.DataSource = tblNhanXetCuaChuTauTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());

                this.tblAssignmentTableAdapter.FillBy_CrewId(dsAssignment.tblAssignment, viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
            }
            if (rdoAssingment.SelectedIndex == -1)
            {
                rdoAssingment.SelectedIndex = 4;
            }
        }

        private void viewCrew_DoubleClick(object sender, EventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {

                splitContainerControl1.Collapsed = false;
            }
        }

        private void btnXem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (viewNhanXet.FocusedRowHandle > -1)
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                string ext = viewNhanXet.GetFocusedRowCellValue("cExt").ToString();
                saveFile.Filter = "FileType|*" + ext.Trim();
                if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    FileStream fs = new FileStream(saveFile.FileName, FileMode.OpenOrCreate, FileAccess.Write);
                    BinaryWriter br = new BinaryWriter(fs);
                    br.Write((byte[])viewNhanXet.GetFocusedRowCellValue("pNoiDung"));
                    fs.Dispose();
                }
            }
        }

        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.tblCrewBindingSource.Filter = "bDeleted = 0 or bDeleted = 1";
            // this.dsCrew.tblCrew.DefaultView.RowFilter = ;
        }

        private void btnActive_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.tblCrewBindingSource.Filter = "bDeleted = 0";
        }

        private void btnInActive_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.tblCrewBindingSource.Filter = "bDeleted = 1";
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {
                if (XtraMessageBox.Show("Bạn chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    viewCrew.SetFocusedRowCellValue("bDeleted", true);
                    tblCrewBindingSource.EndEdit();
                    tblCrewTableAdapter.Update(dsCrew.tblCrew);
                }
            }
        }

        private void gridCrew_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }

        private void rdoAssingment_Properties_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rdoAssingment.SelectedIndex)
            {
                case 0:
                    layoutStartDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutEndDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutStartDate.Text = "Thời gian lên tàu";
                    layoutEndDate.Text = "Thời gian hợp đồng";
                    layoutResult.Text = "Thời gian dự kiến rời tàu";
                    gcAssignment.Enabled = true;
                    break;
                case 1:
                     layoutStartDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutEndDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutStartDate.Text = "Thời gian rời tàu";
                    layoutEndDate.Text = "Thời gian yêu cầu nghỉ";
                    layoutResult.Text = "Thời gian dự kiến sẵn sàng lên tàu";
                    gcAssignment.Enabled = false;
                    break;
                case 2:
                    layoutStartDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutEndDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutResult.Text = "Thời điểm cắt hợp đồng";
                    gcAssignment.Enabled = false;
                    break;
                case 3:
                     layoutStartDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutEndDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutResult.Text = "Thời điểm nghỉ hưu";
                    gcAssignment.Enabled = false;
                    break;
                case 4:
                      layoutStartDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutEndDate.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutResult.Text = "Thời điểm ký hợp đồng tuyển dụng";
                    gcAssignment.Enabled = false;
                    break;
                default:
                    break;
            }
        }

        private void gridCrew_FocusedViewChanged(object sender, DevExpress.XtraGrid.ViewFocusEventArgs e)
        {

           
        }
    }
}